#!/bin/bash
#########################
# 安装 oh-my-zsh
#########################
# tgz file
# file_url="https://gitee.com/sgfoot/library/releases/download/v0.2.0/oh-my-zsh.tgz"
file_url="https://gitee.com/sgfoot/library/releases/download/v0.3.0/oh-my-zsh.tgz"

# get URL name
# 获取文件名
filename=$(basename "${file_url}")

# 获取文件名中的 name 部分
name="${filename%.*}"

work_dir=$HOME

tmp_dir=$(mktemp -d)

#######################################
# 提示
green() {
	local val=$1
	echo -e "\033[32m ${val} \033[0m"
}
# 禁止
red() {
	local val=$1
	echo -e "\033[31m ${val} \033[0m"
}
# 警告
yello() {
	local val=$1
	echo -e "\033[33m ${val} \033[0m"
}
# 判断命令是否存在
check_command() {
	name=$1
	type "${name}" > /dev/null 2>&1
}
##########################################

if curl --output /dev/null --silent --head --fail "${file_url}"; then
    green "${file_url} 连接成功"
else
    red "${file_url} 连接失败"
	exit 0
fi

# Check whether ZSH is installed
yello "Check whether ZSH has been installed"
if which zsh >/dev/null; then
  green "zsh已经安装！"
else
  red "zsh尚未安装！"
  yum -y install zsh
fi

# enter person home
green "enter: ${tmp_dir}"
cd "$tmp_dir" || exit
pwd
green "filename:${filename}"

if [ ! -f "${filename}" ]; then 
    yello "Downloading: ${file_url}"

	if which wget >/dev/null;then
		green "wget 已安装"
	else
		red "wget 未安装"
		yum -y install wget
	fi
	wget -c -O "${filename}" ${file_url}	
fi

tar_file="${tmp_dir}/${filename}"
if [ -f "${tar_file}" ]; then 
    tar -zxvf "${filename}" -C "${work_dir}"
	zsh_file="${work_dir}/.oh-my-zsh/templates/.zshrc"
	if [ ! -f "${zsh_file}" ];then
		red "${zsh_file} 不存在"
		exit 1
	fi
    cp "${zsh_file}" "$work_dir"
    rm -rf "${tmp_dir}"
fi 

result=$?
if [ ${result} -eq 0 ];then 
    green "zsh installed successful"
else
    red "zsh install failed"
fi